<?php

namespace App\Library\TencentMap;

use App\Library\FeiShu\SendFs;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

class CurlResponse extends BaseLibrary
{


    public string $url;
    public array  $requestPrams;
    public string $method;
    public        $response;
    public int    $errNo;
    public int    $responseHttpCode;

    public function init()
    {
        list($this->url, $this->method, $this->requestPrams, $this->errNo, $this->responseHttpCode, $this->response) = $this->config;
    }

    /**
     * 解析curl返回数据
     *
     * @param string $urlTarget
     * @param \Closure $closure
     * @return mixed|void
     */
    public function resolve(string $urlTarget, \Closure $closure)
    {
        if (in_array($this->errNo, [7, 28])) {
            //$no错误码7为连接不上，28为连接上了但请求返回结果超时
            $resolve = false;
        } else {
            $resolve = $closure($this);
        }

        if ($resolve) {
            return $resolve;
        } else {
            SendFs::newRobot()->setTitleContent($urlTarget,
                (new Collection(get_object_vars($this)))
                    ->except('config')
                    ->toJson(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT))
                  ->send();
            Log::error($urlTarget, get_object_vars($this));
            return false;
        }
    }
}